namespace Guia.Elements
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Windows.Automation;
    using Guia.Core;
    using Guia.Locator;

    /// <summary>
    /// This class represents a list-box.
    /// </summary>
    public class ListBox : UiElement
    {
        /// <summary>
        /// Initializes a new instance of the ListBox class. 
        /// </summary>
        /// <param name="automationElement">The AutomationElement representating the ListBox.</param>
        public ListBox(AutomationElement automationElement)
            : base(automationElement)
        {
        }

        /// <summary>
        /// Gets the default-condition to find a control by its type.
        /// </summary>
        public static SearchCondition ControlTypeCondition
        {
            get { return SearchCondition.ByControlType(ControlType.List); }
        }

        /// <summary>
        /// Gets the selected items of the ListBox.
        /// If no item is selected, an empty array is returned.
        /// </summary>
        public ListBoxItem[] SelectedItems
        {
            get
            {
                List<ListBoxItem> selectedItems = new List<ListBoxItem>();
                ListBoxItem[] items = this.Items;
                foreach (ListBoxItem item in items)
                {
                    if (item.IsSelected)
                    {
                        selectedItems.Add(item);
                    }
                }

                return selectedItems.ToArray();   
            }
        }

        /// <summary>
        /// Gets all items of the ListBox.
        /// If there the ListBox is empty, an empty array is returned.
        /// </summary>
        public ListBoxItem[] Items
        {
            get
            {
                return this.GetAll<ListBoxItem>();
            }
        }

        /// <summary>
        /// Gets the ListBoxItem of the ListBox with the given string-value as it's content.
        /// </summary>
        /// <param name="value">
        /// The string-value of the list-item's content.
        /// </param>
        /// <exception cref="ArgumentNullException">This exception is thrown if value is null</exception>
        public ListBoxItem this[string value]
        {
            get
            {
                return this.GetItem(value);
            }
        }

        /// <summary>
        /// Gets the ListBoxItem at the given position.
        /// </summary>
        /// <param name="index">The index of the desired ListBoxItem.</param>
        /// <returns>The ListBoxItem at the given position.</returns>
        /// <exception cref="ElementNotFoundException">This exception is thrown if no ListBoxItem exist at the given position.</exception>
        public ListBoxItem this[int index]
        {
            get
            {
                return this.GetItem(index);
            }
        }

        /// <summary>
        /// Determines whether there is an item with the given string-value as its content.
        /// </summary>
        /// <param name="value">The string-value of the list-item's content.</param>
        /// <returns>true, if the ListBox contains such an item, false otherwise.</returns>
        /// <exception cref="ArgumentNullException">This exception is thrown if value is null</exception>
        public bool Contains(string value)
        {
            Contract.Requires<ArgumentNullException>(value != null);
            bool contains = false;
            try
            {
                var item = this.Get<ListBoxItem>(SearchCondition.ByName(value));
                if (item != null)
                {
                    contains = true;
                }
            }
            catch (ElementNotFoundException)
            {
            }

            return contains;
        }
        
        /// <summary>
        /// Gets the ListBoxItem of the ListBox with the given string-value as it's content.
        /// </summary>
        /// <param name="value">The string-value of the list-item's content.</param>
        /// <returns>The ListBoxItem with the given value</returns>
        /// <exception cref="ElementNotFoundException">An ElementNotFoundException is thrown when there is no item with the given value.</exception>
        /// <exception cref="ArgumentNullException">This exception is thrown if value is null</exception>
        public ListBoxItem GetItem(string value)
        {
            Contract.Requires<ArgumentNullException>(value != null);
            ListBoxItem item = this.Get<ListBoxItem>(SearchCondition.ByName(value));
            return item;
        }

        /// <summary>
        /// Gets the ListBoxItem at the given position.
        /// </summary>
        /// <param name="index">The index of the desired ListBoxItem.</param>
        /// <returns>The ListBoxItem at the given position.</returns>
        /// <exception cref="ElementNotFoundException">This exception is thrown if no ListBoxItem exist at the given position.</exception>
        public ListBoxItem GetItem(int index)
        {
            try
            {
                ListBoxItem[] items = this.Items;
                return items[index];
            }
            catch (IndexOutOfRangeException)
            {
                throw new ElementNotFoundException("No listbox-item found with the index " + index);
            }
        }
    }
}